package com.example.lab.controller;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lab.entity.*;
import com.example.lab.service.LostService;
import com.example.lab.service.LostVoService;
import jdk.nashorn.internal.runtime.PropertyMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * (Lost)表控制层
 *
 * @author makejava
 * @since 2022-04-27 19:20:53
 */
@RestController
@RequestMapping("lost")
public class LostController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private LostService lostService;
    @Resource
    private LostVoService lostVoService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param lostVo 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public MyReturn selectAll(Integer limit, Integer page, LostVo lostVo) {
        Integer page1=(page-1)*limit;
        Integer limit1=limit;
        List<LostVo> lostVos = lostVoService.queryAll(limit1, page1, lostVo);
        /*Page<Lost> lostPage = new Page<>(page, limit);
        QueryWrapper<Lost> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(lost.getLcontent())){
            queryWrapper.like("Lcontent",lost.getLcontent());
        }
        if(ObjectUtil.isNotEmpty(lost.getLuid())){
            queryWrapper.like("LUid",lost.getLuid());
        }
        Page<Lost> page1 = this.lostService.page(lostPage, queryWrapper);
        */
        Long count = lostVoService.getCount();
        return super.success(lostVos,count);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public MyReturn selectOne(@PathVariable Serializable id) {
        return success(this.lostService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param lost 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public MyReturn insert(@RequestBody Lost lost) {
        lost.setLtime(new Date());
        boolean save = this.lostService.save(lost);
        if (save) {
            return super.success(lost, 1L);
        } else {
            return super.failed(lost, 1L);
        }

    }

    /**
     * 修改数据
     *
     * @param lost 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public MyReturn update(@RequestBody Lost lost) {
        boolean update = this.lostService.updateById(lost);
        if (update) {
            return super.success(lost, 1L);
        } else {
            return super.failed(lost, 1L);
        }
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public MyReturn delete(@RequestParam("idList[]") List<Long> idList) {
        boolean b = lostService.removeByIds(idList);
        if (b) {
            return super.success(idList);
        } else {
            return super.failed(idList);
        }
    }
    @RequestMapping("/getAllLostNoPage")
    public MyReturn getAllEquipmentNoPage(){
        List<Lost> list = lostService.list();
        return super.success(list, (long) list.size());
    }
}

